﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eTruck.Helpers;

namespace eTruck.Views.Modules
{
    public partial class Form : System.Web.UI.Page
    {
        protected string returnValue = string.Empty;

        BLL.Modules bll = new BLL.Modules();
        protected void Page_Load(object sender, EventArgs e)
        {
            string type = System.Web.HttpUtility.UrlDecode(Request["Type"]);
            if (type == "Save")
            {
                Save();
            }
            else
            {
                Exists();
            }
        }

        private void Save()
        {
            Model.Modules model = new Model.Modules();
            string ModuleID = System.Web.HttpUtility.UrlDecode(Request["ModuleID"]);
            model.ModuleName = System.Web.HttpUtility.UrlDecode(Request["ModuleName"]);
            try
            {
                model.ParentModuleID = Convert.ToInt32(System.Web.HttpUtility.UrlDecode(Request["ParentModuleID"]));
            }
            catch { }
            model.Desc = System.Web.HttpUtility.UrlDecode(Request["Desc"]);
            model.Url = System.Web.HttpUtility.UrlDecode(Request["URL"]);
            try
            {
                model.SortID = Convert.ToInt32(System.Web.HttpUtility.UrlDecode(Request["SortID"]));
            }
            catch { }

            try
            {
                model.UserID = Convert.ToInt32(CookiesHelper.GetCookie(CommonHelper.LoginCookieName));
            }
            catch { }

            try
            {
                bool result = false;
                if (ModuleID.Length == 0)
                {
                    //create new Model
                    result = bll.Add(model);
                }
                else
                {
                    //update Model Info...
                    model.ModuleID = Convert.ToInt32(ModuleID);
                    result = bll.Update(model);
                }
                if (result)
                {
                    returnValue = "True";
                }
                else
                {
                    returnValue = "False";
                }
            }
            catch (Exception ex)
            {
                returnValue = ex.Message;
            }
        }

        private void Exists()
        {
            string ModuleName = System.Web.HttpUtility.UrlDecode(Request["ModuleName"]);
            if (bll.Exists(ModuleName))
            {
                returnValue = "True";
            }
            else
            {
                returnValue = "False";
            }
        }
    }
}